package dom.domain;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for User
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see dom.domain.User
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class UserDAO {
	private static final Logger log = LoggerFactory.getLogger(UserDAO.class);
	// property constants
	public static final String PSD = "psd";
	public static final String EEID = "eeid";
	public static final String ADMIT_ID = "admitId";
	public static final String ID = "id";
	public static final String EMAIL = "email";
	public static final String PHONE_NO = "phoneNo";
	public static final String USER_GENERAL_TYPE = "userGeneralType";
	public static final String USER_SUB_TYPE = "userSubType";
	public static final String USER_PHOTO = "userPhoto";
	public static final String USER_DESCRIBE = "userDescribe";
	public static final String STATE = "state";
	public static final String BIRTHDAY = "birthday";
	public static final String POLICY = "policy";
	public static final String NATION = "nation";
	public static final String EP_ALLOCATED = "epAllocated";
	public static final String EPYEAR = "epyear";
	public static final String NAME = "name";
	public static final String EP_ID = "epId";
	public static final String PROVINCE_ID = "provinceId";
	public static final String GENDER = "gender";
	public static final String ADMIT_ID_ALLOCATED = "admitIdAllocated";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(User transientInstance) {
		log.debug("saving User instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(User persistentInstance) {
		log.debug("deleting User instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public User findById(java.lang.String id) {
		log.debug("getting User instance with id: " + id);
		try {
			User instance = (User) getCurrentSession().get("dom.domain.User",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(User instance) {
		log.debug("finding User instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("dom.domain.User")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from User as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPsd(Object psd) {
		return findByProperty(PSD, psd);
	}

	public List findByEeid(Object eeid) {
		return findByProperty(EEID, eeid);
	}

	public List findByAdmitId(Object admitId) {
		return findByProperty(ADMIT_ID, admitId);
	}

	public List findById(Object id) {
		return findByProperty(ID, id);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByPhoneNo(Object phoneNo) {
		return findByProperty(PHONE_NO, phoneNo);
	}

	public List findByUserGeneralType(Object userGeneralType) {
		return findByProperty(USER_GENERAL_TYPE, userGeneralType);
	}

	public List findByUserSubType(Object userSubType) {
		return findByProperty(USER_SUB_TYPE, userSubType);
	}

	public List findByUserPhoto(Object userPhoto) {
		return findByProperty(USER_PHOTO, userPhoto);
	}

	public List findByUserDescribe(Object userDescribe) {
		return findByProperty(USER_DESCRIBE, userDescribe);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByBirthday(Object birthday) {
		return findByProperty(BIRTHDAY, birthday);
	}

	public List findByPolicy(Object policy) {
		return findByProperty(POLICY, policy);
	}

	public List findByNation(Object nation) {
		return findByProperty(NATION, nation);
	}

	public List findByEpAllocated(Object epAllocated) {
		return findByProperty(EP_ALLOCATED, epAllocated);
	}

	public List findByEpyear(Object epyear) {
		return findByProperty(EPYEAR, epyear);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByEpId(Object epId) {
		return findByProperty(EP_ID, epId);
	}

	public List findByProvinceId(Object provinceId) {
		return findByProperty(PROVINCE_ID, provinceId);
	}

	public List findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List findByAdmitIdAllocated(Object admitIdAllocated) {
		return findByProperty(ADMIT_ID_ALLOCATED, admitIdAllocated);
	}
	public List query(Object authority,Object state)
	{
		return findByTwoProperty(EP_ALLOCATED,authority,STATE,state);
	}
	public List getList(int offset,int pageSize)
	{

		log.debug("finding all User instances");
		try {
			String queryString = "from User order by userId desc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setMaxResults(pageSize); // 每页最多显示几条
			queryObject.setFirstResult((offset - 1) * pageSize); // 每页从第几条记录开始
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	
	}
	public List findAll() {
		log.debug("finding all User instances");
		try {
			String queryString = "from User";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public User merge(User detachedInstance) {
		log.debug("merging User instance");
		try {
			User result = (User) getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	public List findByTwoProperty(String propertyName, Object value,String propertyName1,Object value1) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value+";" +propertyName1
				+ ", value: " + value1);
		try {
			String queryString = "from User as model where model."
					+ propertyName + "= ? and " +propertyName1+"=?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setParameter(1, value1);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List query(String name)
	{
		try
			{
				String sql = "select * from User as a where a.name like :name";
				Query query = getCurrentSession().createSQLQuery(sql).addEntity(User.class);
				query.setString("name", "%"+name+"%");
				return query.list();
			}catch(RuntimeException e)
			{
				throw e;
		}
		
	}

	public void attachDirty(User instance) {
		log.debug("attaching dirty User instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public long getCount(int admitIdState,String gType,String sType,int provinceId)
	{
		try
		{
			String queryString = "select count(*) from User where "+ADMIT_ID_ALLOCATED +"= ? and "+USER_GENERAL_TYPE+"= ? and "+USER_SUB_TYPE +"= ? and "+PROVINCE_ID+"= ?";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter(0, admitIdState);
			query.setParameter(1, gType);
			query.setParameter(2, sType);
			query.setParameter(3, provinceId);
			return ((Long) query.iterate().next()).intValue();
		}catch(RuntimeException e)
		{
			throw e;
		}
	}
	public void attachClean(User instance) {
		log.debug("attaching clean User instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (UserDAO) ctx.getBean("UserDAO");
	}
}